<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .sort{
      position: relative;
      
    }
    .sort::before,
    .sort::after{
      position: absolute;
      right: -4px;
      transform: scale(0.5);
    }
    .sort::before{
      content: '▲';
      top: -1px;
    }
    .sort::after{
      content: '▼';
      bottom:-2px
    }

    .sort.asc::before,
    .sort.desc::after
    {
      color: red;
    }
  </style>
</head>
<body>
  <button class="sort" onclick="toggle(this)">排序</button>
  <ul id="list"></ul>
  <script>
    var arr=[6,8,9,7,2,4,55,66]
    function view(arr){
      var temp=''
      for(var i=0;i<arr.length;i++){
        temp += '<li>'+arr[i]+'</li>'
      }
      document.getElementById('list').innerHTML=temp
    }
    view(arr)

    function sort(type=1){
      // 将原数组赋值给newarr这个副本
      var newarr=[].concat(arr)
      newarr=newarr.sort(function(a,b){
        return (a-b) * type
      })
      view(newarr)
    }


    var state=0;
    function toggle(button){
      // 如果state为0则赋值为1，如果为1则赋值为-1，否则为0
      state = state === 0?1:state === 1 ? -1 : 0

      var cls={
        0:'sort',
        1:'sort asc',
        '-1':'sort desc'
      }
      // 通过改变按钮的类名，改变样式
      button.className=cls[state];
      // 改变状态后执行排序
      sort(state)
    }
  </script>
</body>
</html>